from twisted.internet import defer, reactor

class HeadlineRetriever(object):
    def processHeadline(self, headline):
        if len(headline) > 50:
            self.d.errback(
                "the headline ``%s`` is to long!"%headline
            )

        else:
            self.d.callback(headline)


    def _toHTML(self, result):
        return "<h1>%s</h1>"%result


    def getHeadline(self, input):
        self.d = defer.Deferred()
        reactor.callLater(1, self.processHeadline, input)
        self.d.addCallback(self._toHTML)
        return  self.d

def printData(result):
    print result
    reactor.stop()

def printError(failure):
    print failure
    reactor.stop()


h = HeadlineRetriever()

d = h.getHeadline("Breaking News: Twisted Takes Us to the Moon!")
# d = h.getHeadline("You are to loong" * 8)

d.addCallbacks(printData, printError)


reactor.run()
